
export default class Shadow extends cc.Node {
    private _sprite: cc.Sprite;
    public isOver: boolean;
    private _life: number;
    private _opacitySpeed: number;
    // private _lbId:cc.Label;
    public renderTexture:cc.RenderTexture;

    // public init(anchorX: number, anchorY: number,
    public init(
        life: number, startOpacity: number, opacitySpeed: number) {
        // this.anchorX = anchorX;
        // this.anchorY = anchorY;
        // this.width = width;
        // this.height = height;
        // this.scaleX = scaleX;
        // this.scaleY = scaleY;
        this._life = life;
        this._opacitySpeed = opacitySpeed;
        this.opacity = startOpacity;
        if (!this._sprite) {
            this._sprite = this.addComponent(cc.Sprite);
        }
        if (!this._sprite.spriteFrame) {
            this._sprite.spriteFrame = new cc.SpriteFrame();
            this._sprite.spriteFrame.setFlipY(true);
        }
        if(!this.renderTexture){
            this.renderTexture = new cc.RenderTexture();
            this._sprite.spriteFrame.setTexture(this.renderTexture);
        }
        
        // if(!this._lbId){
        //     let node = new cc.Node;
        //     this.addChild(node);
        //     this._lbId = node.addComponent(cc.Label);
        //     this._lbId.node.color = cc.Color.BLACK;
        //     this._lbId.fontSize = 40;
        //     this._lbId.string = 'shadow'
        // }
        
        // this._sprite.spriteFrame.getTexture().setFlipY(true);
    }

    public update(dt) {
        if (this.isOver) return;
        if (this._life > 0) {
            this._life -= dt;
        }
        if (this._opacitySpeed) {
            this.opacity += Math.floor(this._opacitySpeed * dt);
        }

        this.isOver = this._life <= 0 || this.opacity <= 0;
    }

    public recover() {
        this.removeFromParent();
        this.isOver = null;
        this._life = null;
        this._opacitySpeed = null;
        if (this.renderTexture) {
            this.renderTexture.clear();
        }
    }
}
